package com.example.system.controller;

import com.example.system.common.Result;
import com.example.system.dto.CurUserInvitationInfo;
import com.example.system.entity.Invitation;
import com.example.system.exception.CustomException;
import com.example.system.service.impl.InvitationServiceImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/invitation")
@MapperScan("com.*.mapper")
public class InvitationController {
    @Autowired
    private InvitationServiceImpl invitationService;

    // inviterId邀请inviteeId
    @PostMapping("/invite/{inviterId}/{inviteeId}")
    public Result invite(@PathVariable Integer inviterId, @PathVariable Integer inviteeId) {
        try {
            invitationService.invite(inviterId, inviteeId);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success();
    }

    // inviteeId接受inviterId的邀请
    @PostMapping("/accept/{inviteeId}/{inviterId}")
    public Result accept(@PathVariable Integer inviteeId, @PathVariable Integer inviterId) {
        try {
            invitationService.accept(inviteeId, inviterId);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success();
    }

    // inviteeId拒绝inviterId的邀请
    @PostMapping("/reject/{inviteeId}/{inviterId}")
    public Result reject(@PathVariable Integer inviteeId, @PathVariable Integer inviterId) {
        try {
            invitationService.reject(inviteeId, inviterId);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success();
    }

    // 获取当前用户的所有邀请记录
    @GetMapping("/search/inviter/{userId}")
    public Result getAllInviterRecordsByUserId(@PathVariable Integer userId) {
        List<CurUserInvitationInfo> allRecords;
        try {
            allRecords = invitationService.getAllInviterRecordsByUserId(userId);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        if (allRecords == null || allRecords.isEmpty()) {
            return Result.error("当前用户无邀请记录");
        }
        return Result.success(allRecords);
    }

    // 获取当前用户的指定被邀请用户id的邀请记录
    @GetMapping("/search/inviter/{userId}/{inviteeId}")
    public Result getAllInviterRecordsByUserIdAndInviteeId(@PathVariable Integer userId, @PathVariable Integer inviteeId) {
        List<CurUserInvitationInfo> allRecords;
        try {
            allRecords = invitationService.getAllInviterRecordsByUserIdAndInviteeId(userId, inviteeId);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        if (allRecords == null || allRecords.isEmpty()) {
            return Result.error("当前用户无邀请记录");
        }
        return Result.success(allRecords);
    }

    // 获取当前用户的所有被邀请记录
    @GetMapping("/search/invitee/{userId}")
    public Result getAllInviteeRecordsByUserId(@PathVariable Integer userId) {
        List<CurUserInvitationInfo> allRecords;
        try {
            allRecords = invitationService.getAllInviteeRecordsByUserId(userId);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        if (allRecords == null || allRecords.isEmpty()) {
            return Result.error("当前用户无被邀请记录");
        }
        return Result.success(allRecords);
    }

    // 获取当前用户的指定邀请用户id的邀请记录
    @GetMapping("/search/invitee/{userId}/{inviterId}")
    public Result getAllInviteeRecordsByUserIdAndInviterId(@PathVariable Integer userId, @PathVariable Integer inviterId) {
        List<CurUserInvitationInfo> allRecords;
        try {
            allRecords = invitationService.getAllInviteeRecordsByUserIdAndInviterId(userId, inviterId);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        if (allRecords == null || allRecords.isEmpty()) {
            return Result.error("当前用户无邀请记录");
        }
        return Result.success(allRecords);
    }

    // 求最新被邀请记录的数量
    @GetMapping("/search/count/invitee/{userId}")
    public Result getAllNewInviteeRecordsCountByUserId(@PathVariable Integer userId) {
        return Result.success(invitationService.getAllNewInviteeRecordsCountByUserId(userId));
    }

    // 删除指定编号的记录
    @DeleteMapping("/delete/{invitationId}")
    public Result deleteOneRecordByInvitationId(@PathVariable Integer invitationId) {
        try {
            invitationService.deleteOneRecordByInvitationId(invitationId);
        } catch (CustomException e) {
            return Result.error(e.getMsg());
        }
        return Result.success();
    }
}
